<?php
class ModelNewsNews extends Model {
	public function addNews($data) {

		$this->db->query("INSERT INTO " . DB_PREFIX . "news SET title = '" . $this->db->escape($data['title']) . "', description = '" . $this->db->escape($data['description']) . "', short_des = '" . $this->db->escape($data['short_des'])."', create_date = ".$this->time->nowtoint());
		$news_info = $this->getNewsByTitle($data['title']);
		$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'new_id=".$news_info['new_id']."' , keyword = '".$data['keyword']."'");
		$this->cache->delete('news');
	}
	
	public function editNews($news_id, $data) {
	
		$this->db->query("DELETE FROM " . DB_PREFIX . "news WHERE new_id = '" . (int)$news_id . "'");				
		$this->db->query("INSERT INTO " . DB_PREFIX . "news SET title = '" . $this->db->escape($data['title']) . "', description = '" . $this->db->escape($data['description']) . "', short_des = '" . $this->db->escape($data['short_des'])."', create_date = ".$this->time->nowtoint());
		$news_info = $this->getNewsByTitle($data['title']);
		$this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'new_id=" . (int)$news_id . "'");
		$this->db->query("INSERT INTO " . DB_PREFIX . "url_alias SET query = 'new_id=".$news_info['new_id']."' , keyword = '".$data['keyword']."'");
		$this->cache->delete('news');
	}
	public function getNewsByTitle($title)
	{
		$query = $this->db->query("SELECT * FROM ".DB_PREFIX."news WHERE title = '".$title."'");
		return $query->row;
	}
	public function deleteNews($news_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "news WHERE new_id = '" . (int)$news_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "url_alias WHERE query = 'news_id=" . (int)$news_id . "'");
		$this->cache->delete('news');
	}	

		
	public function getNews($data = array()) {
		if ($data) {
			$sql = "SELECT * FROM " . DB_PREFIX . "news";
		
			$sort_data = array(
				'title',
				'create_date'
			);		
		
			if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
				$sql .= " ORDER BY " . $data['sort'];	
			} else {
				$sql .= " ORDER BY title";	
			}
			
			if (isset($data['order']) && ($data['order'] == 'DESC')) {
				$sql .= " DESC";
			} else {
				$sql .= " ASC";
			}
		
			if (isset($data['start']) || isset($data['limit'])) {
				if ($data['start'] < 0) {
					$data['start'] = 0;
				}		

				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}	
			
				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
			}	
			
			$query = $this->db->query($sql);
			
			return $query->rows;
		} else {
			$news_data = $this->cache->get('news.' . $this->config->get('config_language_id'));
		
			if (!$news_data) {
				$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news ORDER BY title");
	
				$news_data = $query->rows;
			
				$this->cache->set('news.' . $this->config->get('config_language_id'), $news_data);
			}	
	
			return $news_data;			
		}
	}
	
	public function getNewsInfo($news_id) {
		$news_description_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "news WHERE new_id = '" . (int)$news_id . "'");
		$query2 = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE query = 'news_id=" . (int)$news_id . "'");
			$information_description_data = array(
				'id'		  => $query->row['new_id'],
				'title'       => $query->row['title'],
				'description' => $query->row['description'],
				'short_des'  =>  $query->row['short_des'],
				'keyword'	 =>  isset($query2->row['keyword'])?$query2->row['keyword']: "" 
			);
		return $information_description_data;
	}
	
	
	public function getTotalNews() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "news");
		
		return $query->row['total'];
	}	
}
?>